﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dotFitness.Domain
{
    public abstract class EntityBase
    {
        public int Id
        {
            get;
            private set;
        }

        public bool IsTransient
        {
            get { return this.Id == default(int); }
        }

        public override bool Equals(object obj)
        {
            if (null == obj)
                return false;

            if (!(obj is EntityBase))
                return false;

            if (ReferenceEquals(this, obj))
                return true;

            if (this.IsTransient)
                return false;

            var e = (EntityBase)obj;

            if (e.IsTransient)
                return false;

            return this.Id == e.Id;
        }

        public override int GetHashCode()
        {
            if (this.IsTransient)
                return base.GetHashCode();
            else
                return this.Id.GetHashCode() ^ 9;
        }

        public static bool operator ==(EntityBase left, EntityBase right)
        {
            if (Object.Equals(left, null))
                return (Object.Equals(right, null)) ? true : false;
            else
                return left.Equals(right);
        }

        public static bool operator !=(EntityBase left, EntityBase right)
        {
            return !(left == right);
        }
    }
}
